//
//  LYFVistorView.swift
//  weibohhhh
//
//  Created by fersh on 16/1/4.
//  Copyright © 2016年 fersh. All rights reserved.
//

import UIKit
import SnapKit

class LYFVistorView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViewUI()
        startAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     供外界设置当前访客视图的内容
     */
    
    func setupVistorInfo(imageName: String?,messeageText: String?){
        
        if  imageName != nil{
            
            iconImageView.image = UIImage(named: imageName!)
            messeageLable.text = messeageText
            circleImageView.hidden = true
        }else {
            //如果
            startAnimation()
        }
        
    }
    
    //开启动画
    private func startAnimation(){
        
        let anmi = CABasicAnimation(keyPath: "transform.rotation")
        anmi.duration = 5
        anmi.toValue = 2 * M_PI
        anmi.repeatCount = MAXFLOAT
        anmi.removedOnCompletion = false
        circleImageView.layer.addAnimation(anmi, forKey: nil)
    }
    
    //懒加载子控件
    private lazy  var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy  var circleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy  var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var registerButton: UIButton = {
        
        let button = UIButton(titelColor: UIColor.orangeColor(), fontSize: 15)

        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
    lazy var loginButton: UIButton = {
        
        let button = UIButton(titelColor: UIColor.grayColor(), fontSize: 15)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        return button
    }()
    
    private lazy var messeageLable: UILabel = {
        
        let lable = UILabel(titelColor:UIColor.grayColor(), fontSize: 14)
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.numberOfLines = 0
        lable.textAlignment = .Center
        return lable
    }()
    
    
}

extension  LYFVistorView {
    
    //添加子控件
    private func setupViewUI() {
        
        self.backgroundColor = UIColor(white: 237/255, alpha: 1)
        /**
        *  子控件添加到view上
        */
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        
        addSubview(messeageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        /**
        *  设置iconImageView的约束
        */
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center)
        }
        /**
        *  设置 circleImageView
        */
        
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center)
        }
        /**
        *  设置maskImageView 约束半透明动画
        */
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top)
            make.width.equalTo(snp_width)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
        /**
        *  设置messeageLable约束
        */
        messeageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleImageView.snp_centerX)
            make.top.equalTo(circleImageView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        
        /**
        *  设置注册按钮的约束
        */
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messeageLable.snp_leading)
            make.top.equalTo(messeageLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messeageLable.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(100)
        }
        
    }
    
//    private func setupUI() {
//        
//        self.backgroundColor = UIColor(white: 237/255, alpha: 1)
//        /**
//        *  子控件添加到view上
//        */
//        addSubview(circleImageView)
//        addSubview(maskImageView)
//        addSubview(iconImageView)
//        
//        
//        
//        addSubview(messeageLable)
//        addSubview(registerButton)
//        addSubview(loginButton)
//        
//        /**
//        *  设置iconImageView的约束
//        */
//        iconImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item:iconImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        
//        
//        /**
//        *  设置 circleImageView
//        */
//        circleImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item:circleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        
//        
//        /**
//        *  设置messeageLable约束
//        */
//        messeageLable.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: messeageLable, attribute: .CenterX, relatedBy: .Equal, toItem: circleImageView, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messeageLable, attribute: .Top, relatedBy: .Equal, toItem: circleImageView, attribute: .Bottom, multiplier: 1, constant: 16))
//        // 约束宽度
//        addConstraint(NSLayoutConstraint(item: messeageLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
//        /**
//        *  设置注册按钮的约束
//        */
//        registerButton.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messeageLable, attribute: .Leading, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messeageLable, attribute: .Bottom, multiplier: 1, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
//        
//        /**
//        *  设置登录按钮的约束
//        */
//        loginButton.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messeageLable, attribute: .Trailing, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
//      
//    }
//  
}
